﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CardLib1
{
    public class Deck:ICloneable
    {
        //private Card[] cards;
        private Cards cards = new Cards();
        public Deck()
        {
            //cards = new Card[52];
            for (int suitVal = 0; suitVal < 4; suitVal++)
            {
                for (int rankVal = 1; rankVal < 14; rankVal++)
                {
                    //cards[suitVal * 13 + rankVal - 1] = new Card((Suit)suitVal, (Rank)rankVal);
                    cards.Add(new Card((Suit)suitVal,(Rank)rankVal));
                }
            }
        }

        public Deck(Cards cards)
        {
            this.cards = cards;
        }

        public Card GetCard(int cardNum)
        {
            if (cardNum >= 1 && cardNum <= 52)
                return cards[cardNum - 1];
            else
                throw (new System.ArgumentOutOfRangeException("cardNum", cardNum, "Value must be between 1 and 52"));
        }

        public void Shuffle()
        {
            //Card[] newDeck = new Card[52];
            Cards newDeck=new Cards();
            bool[] assigned = new bool[52];
            Random sourceGen = new Random();
            for (int i = 0; i < 52; i++)
            {
                //  int destCard = 0;
                int sourceCard = 0; //sourceGen.Next(52);
                bool foundCard = false;
                while (foundCard == false)
                {
                    //          destCard = sourceGen.Next(52);
                     sourceCard = sourceGen.Next(52);
                    if (assigned[sourceCard] == false)
                        foundCard = true;
                }

                assigned[sourceCard] = true;
                // newDeck[destCard] = cards[i];
                newDeck.Add(cards[sourceCard]);
            }
            newDeck.CopyTo(cards);
        }


        public void PrintDeck()
        {
            for (int i = 0; i < 52; i++)
            {
                Console.WriteLine("i:" + (i + 1).ToString() + cards[i].ToString());
            }
        }

        public object Clone()
        {
            Deck deck = new Deck(cards.Clone() as Cards);
            return deck;
        }


    }
}